package com.example.teesystem.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.teesystem.entity.User;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;

/**
 * @author pxr
 * @data 2022/3/23 10:02
 */

public class TokenUtil {
    @Value("${json.key}")
    private static String KEYs;
    /**
     * 生成token 的方法
     * @param user
     * @return
     */
    public static String KEY = "mzywckyyds121756xqsbyige@buinawe";
    public static long TIME = 36000;//失效时间12个小时
    public static String getToken(User user){
        String token = "";
        Date expiresDate = new Date(System.currentTimeMillis() + 1000*TIME);
        token = JWT.create().withAudience(String.valueOf(user.getId())).withExpiresAt(expiresDate)
                .sign(Algorithm.HMAC256(KEY));
        return token;
    }

    public static String getKEY(){
        return KEY;
    }

}
